/*
 * EaiManager.java
 *
 */
package com.zll.framework.util;


import java.util.*;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;




/**
 * 
 * @author eastar
 */
public class HttpClientUtil
{
	/*
	 * http request를 POST방식으로 보낸뒤, 결과 값을 설정하고, 처리결과를 리턴한다.
	 * 
	 * @param	eaiName		업무코드		HR:인사관리
	 * @param	msg			EAI Message
	 * @return int			처리결과		1:정상, 9:실패
	 */
	public static String executePost(String url) 
	throws IOException, Exception 
	{
		HttpClient client = new HttpClient();

        PostMethod method = null;
        
        try 
        {
        	client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
        	
            // Create a method instance.
            method = new PostMethod(url);
  
            NameValuePair[] parameter = {
                    new NameValuePair("", "")
                  };
    
            method.setRequestBody(parameter);

            // Provide custom retry handler is necessary
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(3, false));
            
            // Execute the method.
            int statusCode = client.executeMethod(method);


            // statusCode가 정상이 아닌 경우 에러를 반환한다.
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
                System.err.println("Method failed: " + method.getStatusCode());
                throw new HttpException(method.getStatusLine().toString());
            }

            // Read the response body.
            byte[] responseBody = method.getResponseBody();
            
            return new String(responseBody);
            
        } catch (Exception e) {
			e.printStackTrace();
            throw e;
        } finally {
            if(method != null) {
                try {
                    method.releaseConnection();
                } catch (Exception e){}
            }
        }	  
	}  
	
	public static String executeGet(String url) 
	{
		return executeGet(url, "");
	}
	/**
	 * HTTP REQUEST를 GET방식으로 보낸뒤 결과를 리턴한다. 
	 * @param url
	 * @return
	 */
	public static String executeGet(String url, String charset) 
	{
		String html = "";
		
		
		
		HttpClient client = new HttpClient();
				
		GetMethod method = new GetMethod(url);
		
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
			new DefaultHttpMethodRetryHandler(3, false));
		
		try {
			int statusCode = client.executeMethod(method);
			
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}
			
			byte[] responseBody = method.getResponseBody();
			
			if ("".equals(charset)) {
				html = new String(responseBody);
			} else {
				html = new String(responseBody, charset);
			}
			
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}  
		
		return html;
	}
	
	
	
}





